//
//  ToturialView.m
//  Unity-iPhone
//
//  Created by Karl on 2017/3/28.
//
//

#import "ToturialView.h"
#import "ButtomLogoView.h"
#import "ToturialPageView.h"

@interface ToturialView ()<UIScrollViewDelegate>

@end

@implementation ToturialView{
    ButtomLogoView *_butView;
    UIScrollView *_scrollBg;
    
    ToturialPageView *_page1;
    ToturialPageView *_page2;
    
    UIButton *_closeBtn;
    UIPageControl *_page;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        
        self.backgroundColor = [UIColor colorWithRed:142.0/255.0 green:176.0/255.0 blue:198.0/255.0 alpha:1.0];

        
        _scrollBg = [[UIScrollView alloc] init];
        [_scrollBg setPagingEnabled:YES];
        [_scrollBg setDelegate:self];
        [self addSubview:_scrollBg];
        
        _page1 = [[ToturialPageView alloc] initWithDictionary:
                  @{@"title":@{@"image":Babel(@"tutorial_1_title"),
                               @"top":@(55.7),
                               @"width":@(220),
                               @"height":@(30.0)},
                    @"left":@{@"image":@"tutorial_1_image",
                              @"left":@(22.7),
                              @"width":@(283.3),
                              @"height":@(300.7)},
                    @"right":@{@"image":Babel(@"tutorial_1_text"),
                               @"right":@(22.7),
                               @"bottom":@(114),
                               @"width":@(400),
                               @"height":@(178.7)}}];
        [_scrollBg addSubview:_page1];
        
        _page2 = [[ToturialPageView alloc] initWithDictionary:
                  @{@"title":@{@"image":Babel(@"tutorial_2_title"),
                               @"top":@(55.7),
                               @"width":@(220),
                               @"height":@(30.0)},
                    @"right":@{@"image":@"tutorial_2_image",
                              @"right":@(22.7),
                              @"bottom":@(0),
                              @"width":@(283.3),
                              @"height":@(300.7)},
                    @"left":@{@"image":Babel(@"tutorial_2_text"),
                               @"left":@(22.7),
                               @"bottom":@(114),
                               @"width":@(400),
                               @"height":@(178.7)}}];
        [_scrollBg addSubview:_page2];
        
        _closeBtn = [UIButton new];
        [_closeBtn setBackgroundImage:[UIImage imageNamed:@"all_close"] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeBtnOnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_closeBtn];
        
        _page = [[UIPageControl alloc] init];
        _page.numberOfPages = 2;
        _page.currentPage = 0;
        [self addSubview:_page];
        
        _butView = [[ButtomLogoView alloc] init];
        [self addSubview:_butView];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat scale = 1.0;
    if (isPad) {
        scale = SizeScaleX;
    }else{
        scale = SizeScaleY;
    }
    
    _scrollBg.left = 0;
    _scrollBg.top = 0;
    _scrollBg.width = self.width;
    _scrollBg.height = self.height;
    [_scrollBg setContentSize:CGSizeMake(_scrollBg.width*2, _scrollBg.height)];
    
    _page1.left = 0;
    _page1.top = 0;
    _page1.width = self.width;
    _page1.height = self.height;
    
    _page2.left = _page1.right;
    _page2.top = 0;
    _page2.width = self.width;
    _page2.height = self.height;
    
    _closeBtn.width = 30.0;
    _closeBtn.height = 30.0;
    _closeBtn.top = self.top + 6.7;
    _closeBtn.right  =self.right - 6.7;
    
    _butView.left = 0;
    _butView.width = self.width;
    _butView.height = 66 * scale;
    _butView.bottom = self.bottom;
    
    _page.centerX = self.width/2;
    _page.bottom = _butView.top -33 * scale;
    _page.height = 10 * scale;
    _page.width = 50 * scale;
}

- (void)closeBtnOnPressed:(id)sender{
    NSLog(@"close toturial");
    [self setHidden:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == _scrollBg) {
        CGPoint offset = scrollView.contentOffset;
        CGRect bounds = scrollView.frame;
        [_page setCurrentPage:offset.x/bounds.size.width];
    }
}

@end
